package au.com.bluelink.scjp.chap6;

import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import java.text.DateFormat;

public class DateTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Date d = new Date();
		System.out.println("The current plain date(d.toString()): "
				+ d.toString());

		TestDates1.test();
		TestDates2.test();
		TestDates3.test();
		TestDates4.test();
	}

}

class TestDates1 {
	public static void test() {
		Date d1 = new Date(1000000000000L); // a trillion!
		System.out.println("1st date " + d1.toString());

		Calendar c = Calendar.getInstance();
		c.setTime(d1); // #1
		if (c.SUNDAY == c.getFirstDayOfWeek()) // #2
			System.out.println("Sunday is the first day of the week");
		System.out.println("trillionth milli day of week is "
				+ c.get(c.DAY_OF_WEEK)); // #3

		c.add(Calendar.MONTH, 1); // #4

		Date d2 = c.getTime(); // #5
		System.out.println("new time " + d2.toString());
	}
}

class TestDates2 {
	public static void test() {
		Date d1 = new Date(1000000000000L);
		DateFormat[] dfa = new DateFormat[6];
		dfa[0] = DateFormat.getInstance();
		dfa[1] = DateFormat.getDateInstance();
		dfa[2] = DateFormat.getDateInstance(DateFormat.SHORT);
		dfa[3] = DateFormat.getDateInstance(DateFormat.MEDIUM);
		dfa[4] = DateFormat.getDateInstance(DateFormat.LONG);
		dfa[5] = DateFormat.getDateInstance(DateFormat.FULL);
		for (DateFormat df : dfa)
			System.out.println(df.format(d1));
	}
}

class TestDates3 {
	public static void test() {
		Calendar c = Calendar.getInstance();
		c.set(2010, 11, 14); // December 14, 2010
		// (month is 0-based
		Date d2 = c.getTime();
		Locale locIT = new Locale("it", "IT"); // Italy
		Locale locPT = new Locale("pt"); // Portugal
		Locale locBR = new Locale("pt", "BR"); // Brazil
		Locale locIN = new Locale("hi", "IN"); // India
		Locale locJA = new Locale("ja"); // Japan

		// code comes from a US book, so short sighted LOL...
		DateFormat dfUS = DateFormat.getInstance();
		System.out.println("US " + dfUS.format(d2));
		DateFormat dfUSfull = DateFormat.getDateInstance(DateFormat.FULL);
		System.out.println("US full " + dfUSfull.format(d2));
		DateFormat dfIT = DateFormat.getDateInstance(DateFormat.FULL, locIT);
		System.out.println("Italy " + dfIT.format(d2));
		DateFormat dfPT = DateFormat.getDateInstance(DateFormat.FULL, locPT);
		System.out.println("Portugal " + dfPT.format(d2));
		DateFormat dfBR = DateFormat.getDateInstance(DateFormat.FULL, locBR);
		System.out.println("Brazil " + dfBR.format(d2));
		DateFormat dfIN = DateFormat.getDateInstance(DateFormat.FULL, locIN);
		System.out.println("India " + dfIN.format(d2));
		DateFormat dfJA = DateFormat.getDateInstance(DateFormat.FULL, locJA);
		System.out.println("Japan " + dfJA.format(d2));
	}
}

class TestDates4 {
	public static void test() {
		Calendar c = Calendar.getInstance();
		c.set(2010, 11, 14);
		Date d2 = c.getTime();
		
		Locale locBR = new Locale("pt", "BR"); // Brazil
		Locale locDK = new Locale("da", "DK"); // Denmark
		Locale locIT = new Locale("it", "IT"); // Italy
		
		
		System.out.println("The current default locale: " + Locale.getDefault());
		
		System.out.println("def " + locBR.getDisplayCountry());
		System.out.println("loc " + locBR.getDisplayCountry(locBR));
		System.out.println("def " + locDK.getDisplayLanguage());
		System.out.println("loc " + locDK.getDisplayLanguage(locDK));
		System.out.println("D>I " + locDK.getDisplayLanguage(locIT));
	}
}
